package com.personnalManagerSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.personnalManagerSystem.model.*;
import com.personnalManagerSystem.service.IPositionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liqing
 * @since 2022-03-31
 */
@Controller
@RequestMapping("/position")
public class PositionController extends BaseController{
    //重写分页
    public  Model baseList(Model model,
                           String option,
                           String searchContent,
                           Integer pageNum,
                           Integer pageSize,
                           IPositionService iPositionService){
        //设置默认的页码
        if(pageNum ==null || pageNum==0){
            pageNum=1;
        }
        //设置一页默认的显示记录
        if(pageSize==null  || pageSize==0){
            pageSize=5;
        }
        //创建一个构造器
        QueryWrapper<Position2> wrapper = new QueryWrapper<>();
        if(option!=null
                && searchContent!=null
                && !"".equals(option)
                && !"".equals(searchContent)){
            wrapper.like(option,searchContent);
        }

        //设置分页
        Page<List<Position2>> pageInfo = PageHelper.startPage(pageNum, pageSize);
        //查询数据
        iPositionService.queryPositionItem(wrapper);
        //设置页面信息
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("option",option);
        model.addAttribute("searchContent",searchContent);
        return model;
    }



    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(value = "option",required = false) String option,
                       @RequestParam(value = "searchContent",required = false) String searchContent,
                       Integer pageNum,
                       Integer pageSize){
       /* System.out.println("option:"+option);
        System.out.println("searchContent:"+searchContent);*/
        baseList(model,option,searchContent,pageNum,pageSize,iPositionService);
        List<Department> departmentList = iDepartmentService.list();
        model.addAttribute("departmentList",departmentList);
        List<DepartmentPosition> departmentPositionList = iDepartmentPositionService.list();
        model.addAttribute("departmentPositionList",departmentPositionList);
        return "view/position";

    }
    @RequestMapping("/del")
    /**
     * @Description  删除数据,错误的操作先删除单表,然后根据id删除相关联的表,以至于
     * 完成了一个单表的操作,第三张表因参数为空,总是失败
     * @author liqing
     * @data 2022/4/1  0:29
     * @param positionId
     * @return java.lang.String
     */
    public String del(@RequestParam("positionId")Integer positionId){
        System.out.println("狗狗汪小黄");
        System.out.println("positionId:"+positionId);
        //根据职位id删除职位表中的记录,并且删除部门职位表中的记录,用来维护数据第三张表中的数据
        //一个部门对应多个职位,一个职位至多对应一个部门,所以根据职位id直接删除数据
        iDepartmentPositionService.removeById(positionId);
        //删除职位表中的数据
        iPositionService.removeById(positionId);

        return "redirect:/position/list";
    }

    @RequestMapping("/checkPositionName")
    @ResponseBody
    /**
     * @Description 在添加职位之前,先对职位名称进行唯一性校验
     * @author liqing
     * @data 2022/4/2  14:51
     * @param positionName
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public Map<String,Object> checkPositionName(@RequestParam("positionName")String positionName){
        Map<String,Object> map = new HashMap<>();
        Position position=iPositionService.checkPositionName(positionName);
        if(position==null){
            map.put("code",200);
            map.put("msg","该职位名称不存在");
        }else{
            map.put("code",201);
            map.put("msg","该职位名称已经存在,请重新输入");
        }
        return map;
    }
    @RequestMapping("/add")
    /**
     * @Description 这里在添加职位时,同时维护了第三种表的数据,以后操作同上,不用再进行手动添加
     * @author liqing
     * @data 2022/4/1  0:28
     * @param departmentId
     * @param positionName
     * @param positionCode
     * @return java.lang.String
     */
    public String add(@RequestParam(value = "departmentId",required = false) Integer departmentId,
                      @RequestParam(value = "positionName",required = false) String positionName,
                      @RequestParam(value = "positionCode",required = false)String positionCode){
        //System.out.println("departmentId:"+departmentId);
        //职位信息存入职位表中
        Position position = new Position();
        position.setPositionName(positionName);
        position.setPositionCode(positionCode);
        iPositionService.save(position);
        //然后维护部门职位表中的数据,创建部门职位对象
        DepartmentPosition departmentPosition = new DepartmentPosition();
        //设置部门id
        departmentPosition.setDepartmentId(departmentId);
        List<Position> positionList = iPositionService.list();
        List<Integer> positionIds = positionList.stream()
                .map(Position::getPositionId)
                .collect(Collectors.toList());  //为了保证得到的职位id和插入的id保持一致,你不要又整成set
        //得到职位表中的职位id表
        Integer positionId = positionIds.get(positionIds.size() - 1);
        //设置职位id
        departmentPosition.setPositionId(positionId);
        System.out.println("departmentPosition:"+departmentPosition);
        //把部门id和职位id存入部门职位表
        iDepartmentPositionService.save(departmentPosition);
        return "redirect:/position/list";
    }

    @RequestMapping("/edit")
    public String  edit(@RequestParam(value = "positionId",required = false)Integer positionId,
                        @RequestParam(value = "departmentId",required = false)Integer departmentId,
                        @RequestParam(value = "positionName",required = false) String positionName,
                        @RequestParam(value = "positionCode",required = false)String positionCode){
        Position position = new Position();
        //set注入
        position.setPositionId(positionId);
        position.setPositionName(positionName);
        position.setPositionCode(positionCode);
        iPositionService.saveOrUpdate(position);
        //根据传入的职位id获取部门职位对象
        DepartmentPosition departmentPosition = new DepartmentPosition();
        List<Integer> departmentPositionIds = iDepartmentPositionService.list().stream().map(DepartmentPosition::getDepartmentPositionId).collect(Collectors.toList());
        Integer departmentPositionId = departmentPositionIds.get(departmentPositionIds.size() - 1);
        //部门职位set注入
        departmentPosition.setDepartmentPositionId(departmentPositionId);
        departmentPosition.setDepartmentId(departmentId);
        departmentPosition.setPositionId(positionId);
        //对部门职位表中的记录进行修改
        iDepartmentPositionService.saveOrUpdate(departmentPosition);
        return "redirect:/position/list";
    }



}

